﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Firefly;
using Firefly.Libs;
using Ether;
using Ether.Mapping;
using Ether.Libs;
using Jitter;
using Jitter.Collision;

namespace Guardian
{
    public sealed class RenderTest01 : GameScreen
    {
        public static string Key = "RT01";
        private enum RTRenderMode
        {
            Solid = 0,
            BB = 1,
        }

        //Fields 
        private DebugCamera camera;
        private SpriteFont font;

        private DebugDrawer dDrawer;
        private RTRenderMode RenderMode;
        private AxisIndicator axis;

        public LevelMap levelMap;
        public World gameWorld;

        //Construct
        public RenderTest01(Game game)
            : base(game)
        {
            gameWorld = new World(new CollisionSystemSAP());
            gameWorld.Gravity = new Jitter.LinearMath.JVector(0f, -9.7831f, 0f);
        }

        //Methods
        public override bool LoadAssets()
        {
            font = Game.Content.Load<SpriteFont>("default");
            this.LoadingScreen = new LoadScreen(Game, font, Color.White, Color.Black);
            this.LoadingScreen.Manager = this.Manager;

            camera = new DebugCamera(Game, new Vector3(0, 10, -10));
            axis = new AxisIndicator(Game, true, 2f);

            dDrawer = new DebugDrawer(Game);
            dDrawer.Initialize();

            return true;
        }
        public override bool LoadLogic()
        {
            levelMap = new LevelMap(gameWorld);

            int[,] teir1 = new int[,] {
                {0, 0, 0},
                {0, 0, 3},
                {0, 1, 0},
                {0, 3, 0},
                {3, 3, 2}, 
            };
            int[,] teir2 = new int[,] {
                {0, 3, 3},
                {3, 3, 3},
                {3, 3, 3},
                {3, 3, 1},
                {0, 0, 0}, 
            };
            int[,] teir3 = new int[,] {
                {3, 3, 0},
                {0, 0, 0},
                {3, 0, 3},
                {3, 3, 3},
                {3, 3, 3}, 
            };

            levelMap.AddTier(teir1);
            levelMap.AddTier(teir2);
            levelMap.AddTier(teir3);

            //Rotate the Ramp cell to face North
            levelMap[0][2, 1].SetRotation(CellRotation.North);
            levelMap[1][3, 2].SetRotation(CellRotation.South);
            //Set Textures
            levelMap[1][4, 0].SetTextureType(CellTextureType.Snow);
            levelMap[1][4, 1].SetTextureType(CellTextureType.Snow);
            levelMap[1][4, 2].SetTextureType(CellTextureType.Snow);
            
            SelectionEngine.Clear();
            SelectionEngine.Register(levelMap.GetCells());

            gameWorld.Clear();            

            return true;
        }

        public override void RenderGame(SpriteBatch batch, GameTime time)
        {
            axis.Draw(camera, time);

            if (RenderMode == RTRenderMode.Solid) {
                levelMap.DrawLevelMap(camera, time);
            }
            else  {
                levelMap.DebugDrawLevelMap(dDrawer);

                dDrawer.Draw(camera, time);
            }
        }
        public override void UpdateGame(Input input, GameTime time)
        {
            float step = (float)time.ElapsedGameTime.TotalSeconds;
            if (step > 1.0f / 100.0f)
                step = 1.0f / 100.0f;

            gameWorld.Step(step, true);

            camera.UpdateCamera(input);
            levelMap.UpdateLevelMap(time);

            SelectionEngine.SolveSelection(Game, gameWorld, camera, input);
            if (input.IsNewKeyPress(Keys.Space))
                Manager.GoMainMenu();
        }

        public override void RenderUI(SpriteBatch batch, GameTime time)
        {
            batch.Begin();

            batch.DrawString(font, "Hover: " + SelectionEngine.CurrentHover, new Vector2(0, 0), Color.White);
            batch.DrawString(font, "Hover Position: " + SelectionEngine.CurrentHoverPosition, new Vector2(0, 20), Color.White);
            batch.DrawString(font, "Hover EtherPosition: " + SelectionEngine.HoverEtherPosition.ToString(), new Vector2(0, 40), Color.White);

            string rtText = "Render Test 01: Map Testing";
            batch.DrawString(font, rtText, new Vector2(Manager.CenterTextX(font, rtText), Manager.WindowRect.Height - 25), Color.White);
            batch.End();
        }
        public override void UpdateUI(Input input, GameTime time)
        {
            if (input.IsNewKeyPress(Keys.Add))
                levelMap.ShowAnotherTier();
            else if (input.IsNewKeyPress(Keys.Subtract))
                levelMap.HideTopTier();


            if (input.IsNewKeyPress(Keys.L))
                RenderMode = (RenderMode == RTRenderMode.Solid) ? RTRenderMode.BB : RTRenderMode.Solid;
        }
    }
}
